using System.Windows.Forms;
using NUnit.Framework;
using ShadeTree.Binding.Elements;

namespace ShadeTree.Binding.Tests.Elements
{
    [TestFixture]
    public class RadioElementTester
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
        }

        #endregion

        [Test]
        public void BindValuesToRadioButton()
        {
            var target = new EnumTarget();
            target.State = FakeEnum.TX;
            Accessor property = SingleProperty.Build<EnumTarget>(x => x.State);

            var button = new RadioButton();
            var element =
                new RadioElement<FakeEnum>(property, button, new RadioButtonGroup<FakeEnum>());

            element.BoundValue = FakeEnum.TX;
            element.Bind(target);
            Assert.IsTrue(button.Checked);

            target.State = FakeEnum.MO;
            element.Bind(target);
            Assert.IsFalse(button.Checked);

            target.State = FakeEnum.MO;
            element.Bind(target);
            button.Checked = true;
            element.ApplyChanges();
            Assert.AreEqual(FakeEnum.TX, target.State);
        }

        [Test]
        public void ChangeEventHandlerFiresOnCheckChanged()
        {
            bool IwasCalled1 = false;
            bool IwasCalled2 = false;

            var target = new EnumTarget();
            target.State = FakeEnum.TX;
            Accessor property = SingleProperty.Build<EnumTarget>(x => x.State);

            var button = new RadioButton();
            var element =
                new RadioElement<FakeEnum>(property, button, new RadioButtonGroup<FakeEnum>());

            element.BoundValue = FakeEnum.TX;
            element.Bind(target);
            element.RegisterChangeHandler(( ) => IwasCalled1 = true);
            element.RegisterChangeHandler(( ) => IwasCalled2 = true);

            button.Checked = !button.Checked;

            Assert.IsTrue(IwasCalled1);
            Assert.IsTrue(IwasCalled2);
        }

        [Test]
        public void SetTheAliasValueForRadioButtonFromTheText()
        {
            var target = new EnumTarget();
            target.State = FakeEnum.TX;
            Accessor property = SingleProperty.Build<EnumTarget>(x => x.State);

            var button = new RadioButton();
            button.Text = "purple";

            var element =
                new RadioElement<FakeEnum>(property, button, new RadioButtonGroup<FakeEnum>());

            element.Matches("purple").ShouldBeTrue();
        }
    }

    internal enum FakeEnum
    {
        TX,
        CT,
        MO
    }

    internal class EnumTarget
    {
        public FakeEnum State { get; set; }
    }
}